<?php
	require 'page.class.php';
	$connect=mysql_connect("localhost","root","192510");
	mysql_select_db("news",$connect);
	
	$sql2="select count(*) as amount from shouzhong";
	$result2=mysql_query($sql2);
	$row2=mysql_fetch_array($result2);
	$count=$row2['amount'];//总条数
	$pagesize=10;//每页显示的条数
	
	if($count%$pagesize){
		$page=ceil($count/$pagesize);
	}else{
		$page=$count/$pagesize;
	}
	if(isset($_GET['currentpage'])){
		$currentpage=intval($_GET['currentpage']);
    }else{
		$currentpage=1;
    }
	
	$start=($currentpage-1)*$pagesize;
    $sql="select * from shouzhong limit $start,$pagesize";
    $result = mysql_query($sql);
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.6.3.min.js"></script>
<style>
      .tip{
		  width:600px;
		  height:500px;
	  }
	  
</style>

<script type="text/javascript">
$(document).ready(function(){
  $(".click").click(function(){
  $(".tip").fadeIn(200);
  });
  
  $(".tiptop a").click(function(){
  $(".tip").fadeOut(200);
});

  $(".sure").click(function(){
  $(".tip").fadeOut(100);
});

  $(".cancel").click(function(){
  $(".tip").fadeOut(100);
});

});
</script>
</head>

<body>

	<div class="place">
    <span>位置：</span>
    <ul class="placeul">
    <li><a href="#">接收受众</a></li>
    <li><a href="#">受众列表</a></li>
    </ul>
    </div>
    
    <div class="rightinfo">
    
    <div class="tools">
    	<ul class="toolbar">
        <li onclick="ck(0)"><span><img src="images/t01.png" /></span>添加</li>
        <li onclick="deletebatch()"><span><img src="images/t03.png" /></span>删除</li>
        </ul>
    </div>
    
    
    <table class="tablelist">
    	<thead>
    	<tr>
        <th><input type="checkbox"  onclick="selectAll(this)"/></th>
        <th>名称</i></th>
        <th>类型</th>
        <th>邮箱</th>
        <th>licesneID</th>
        <th>备注</th>
        <th>基本操作</th>
        </tr>
        </thead>
		
        <tbody>
	    <?php
		while($row=mysql_fetch_array($result)){
		?>
		<tr>
        <td><input type="checkbox" name="son_checkbox" id="son_checkbox" value="<?php echo $row['id'];?>"/></td>
        <td><?php echo $row['name']; ?></td>
        <td><?php if($row['type']==0){echo "用户";}else{ echo "机器";}?></td>
        <td><?php echo $row['email'];?></td>
        <td><?php echo $row['licesneID'];?></td>
        <td><?php echo $row['beizhu'];?></td>
        <td><a onclick="ck('<?php echo $row['id'];?>')" href="#" class="tablelink">修改</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="delete_shouzhong.php?id=<?php echo $row['id'];?>" class="tablelink">删除</a></td>
        </tr>		
		<?php 
		}
		?>
        </tbody>
    </table>
	
    <div class="pagin">
    	<?php  
    		pagelinks();
    	?>
    </div>
    
    
    <div class="tip">
       <div class="tiptop"><span>受众栏目</span><a></a></div>
       <div class="formbody">
	   <form method="post" action="insert_shouzhong_commit.php">
	   <input size="20" type="hidden" name="id" id="id">
       <ul class="forminfo">
       <li><label>名称</label><input  id="name" name="name" type="text" value="<?php echo $row['name'];?>"  onblur="shouzhong_name();" class="dfinput" /><i id="yz_name" style="display:none">名称不能为空</i></li>
       <li><label>类型</label><select class="dfinput" name="type" id="type"><option value="0"  <?php echo (isset($row)&&($row['type']==0))?'selected=selected':'';?>  onclick="user();">用户</option><option value="1"  <?php echo (isset($row)&&($row['type']==1))?'selected=selected':'';?> onclick="jiqi();">机器</option></select></li>
       <li id="yx"><label>邮箱</label><input id="email" name="email" onblur="yz_email();" type="text" value="<?php echo $row['email'];?>" class="dfinput"/><i id="yz_email_name" style="display:none">邮箱格式不符合规格</i></li>
	   <li id="lic" style="display:none"><label>licesneID</label><input id="licesneID" name="licesneID" value="<?php echo $row['licesneID'];?>" type="text" class="dfinput"/></li>
	   <li id="bz" style="display:none"><label>备注</label><textarea style="width:330px;heigth:30px;" id="beizhu" name="beizhu" cols="20" rows="30" class="textinput"><?php echo $row['beizhu'];?></textarea></li>
       <li><label>&nbsp;</label><input name="" type="submit" class="btn" value="确认保存" onclick="return tijiao();"/></li>
       </ul>
       </form>
       </div>
	   
    </div>
	
	

    </div>
    
    <script type="text/javascript">
	$('.tablelist tbody tr:odd').addClass('odd');
	
	function selectAll(objs){
		
		//获得所有的子选项
	   var obj=document.getElementsByName('son_checkbox');
	   
	   for(var i=0;i<obj.length;i++){
		   if(objs.checked){
			   
			   obj[i].checked=true;
			   
		   }else{
			   
			   obj[i].checked=false;
		   }
		   
	   }
		if(objs.checked){
			//让子选项选中
			
			
		}else{
			//让子选中取消选中
			
		}
		
	}
	
	function deletebatch(){
		
		obj=document.getElementsByName('son_checkbox');
		var arrayid=new Array();
		for(var i=0;i<obj.length;i++){
			if(obj[i].checked){
				
				arrayid.push(obj[i].value);
			}
			
		}
		if(arrayid.length==0){
			alert("请选择需要删除的任务");
		}else{
			  
		   if(confirm("确定要删除所选择的受众吗")){
				$.post('delete_shouzhong.php',{arrayid:arrayid},function(data){
					window.location.reload();
		        });
				  
			}else{
				  return false;
			}
			  
		  }
	}
	
	 function yz_email(){
		  //得到输入的值
		     var email=$("#email").val();
		     var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
			 
			 if(!reg.test(email)){
				 $("#yz_email_name").show();
			 }else{
				 
				 $("#yz_email_name").hide();
			 }
	   }
		  
   function jiqi(){
	   
		$("#lic").show();
		$("#bz").show();
		$("#yx").hide();
		
   }

   function user(){
	   
	   $("#yx").show();
	   $("#lic").hide();
	   $("#bz").hide();
	   
   }

   function shouzhong_name(){
	    var name=$("#name").val();
		  if(name==null||name.length==0){
			  $("#yz_name").show();
			  
		  }else{
			  $("#yz_name").hide();
		  }
		
   }
   
  
		function tijiao(){
		    var name=$("#name").val();
			if(name==null||name.length==0){
				alert("受众的名称不能为空");
				return false;
			}
			
			var email=$("#email").val();
			
			var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
			if(email==null||email.length==0){
			
			}else{
				
				 if(!reg.test(email)){//说明输入是错误的,无法提交
					 alert("请输入正确的邮箱格式");
					 return false;
			   } 
				
			}
			
			return true;
			
	   }
	   
	   
	   function ck(id){
		   //说明是添加
		    if(id==0){
			   $("#name").val("");
			   $("#type").val("");
			   $("#email").val("");
			   $("#licesneID").val("");
			   $("#beizhu").val("");
			   $("#id").val("");
				
			}else{  //说明是编辑
				$.post("select_shouzhong_id.php",{id:id},function(data){
					$("#id").val(data['id']);
					$("#name").val(data['name']);
					$("#type").val(data['type']);
					$("#email").val(data['email']);
					$("#licesneID").val(data['licesneID']);
					$("#beizhu").val(data['beizhu']);
					var type=data['type'];
				    if(type==0){
			          $("#yx").show();
					  $("#lic").hide();
					  $("#bz").hide();
			   
		           }else{
					  $("#yx").hide();
		              $("#lic").show();
		              $("#bz").show();
		          }
			     },"json")
			}
			 $(".tip").fadeIn(200);
			
	   }
	   
	</script>
</body>
</html>
